package ncut.shumei.controller;

import io.swagger.annotations.*;
import ncut.shumei.dto.*;
import ncut.shumei.service.MaintenanceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/tasks")
@Api(tags = "维护任务")
public class MaintenanceTaskController {

    @Autowired
    private MaintenanceTaskService taskService;

    @ApiOperation("获取任务列表")
    @GetMapping
    public ResponseEntity<List<MaintenanceTaskDTO>> getTasks(
            @ApiParam("任务状态") @RequestParam(required = false) String status,
            @ApiParam("电梯ID") @RequestParam(required = false) Integer elevatorId,
            @ApiParam("维护人员ID") @RequestParam(required = false) Integer maintainerId) {
        return ResponseEntity.ok(taskService.getTasks(status, elevatorId, maintainerId));
    }

    @ApiOperation("获取任务详情")
    @GetMapping("/{id}")
    public ResponseEntity<MaintenanceTaskDTO> getTask(@PathVariable Integer id) {
        return ResponseEntity.ok(taskService.getTask(id));
    }

    @ApiOperation("创建维护任务")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<MaintenanceTaskDTO> createTask(@RequestBody MaintenanceTaskCreateRequest request) {
        return ResponseEntity.ok(taskService.createTask(request));
    }

    @ApiOperation("更新维护任务")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<MaintenanceTaskDTO> updateTask(
            @PathVariable Integer id,
            @RequestBody MaintenanceTaskCreateRequest request) {
        return ResponseEntity.ok(taskService.updateTask(id, request));
    }

    @ApiOperation("删除维护任务")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Void> deleteTask(@PathVariable Integer id) {
        taskService.deleteTask(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("更新任务状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTAINER')")
    public ResponseEntity<Void> updateStatus(
            @PathVariable Integer id,
            @RequestBody MaintenanceTaskStatusRequest request,
            @AuthenticationPrincipal UserDetails userDetails) {
        taskService.updateTaskStatus(id, request.getStatus(), Integer.valueOf(userDetails.getUsername()));
        return ResponseEntity.ok().build();
    }

    @ApiOperation("添加任务评论")
    @PostMapping("/{id}/comments")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTAINER')")
    public ResponseEntity<Void> addComment(
            @PathVariable Integer id,
            @RequestBody TaskCommentRequest request,
            @AuthenticationPrincipal UserDetails userDetails) {
        taskService.addComment(id, request.getContent(), Integer.valueOf(userDetails.getUsername()));
        return ResponseEntity.ok().build();
    }
} 